package com.itita.ww2.data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSDataType;
import com.smartfoxserver.v2.entities.data.SFSDataWrapper;
import com.smartfoxserver.v2.entities.data.SFSObject;

/**
 * 大地图数据(默认关卡)
 * @author NorthLan
 *
 */
public class WorldMapConfig {
	public static boolean isLoaded = false;
	/*
	 * 数据
	 */
	private static ISFSObject config; // 配置
	private static ISFSArray blockList; // 区块列表(配置)
	private static ISFSObject cityList; // 城池列表
	private static ISFSObject baseList; // 据点列表
	private static ISFSObject baseMap; // 据点地图数据

	public static void init(ISFSObject config, ISFSObject blockList, ISFSObject cityList, ISFSObject baseList,
			ISFSObject baseMap) {
		WorldMapConfig.config = config.getSFSObject("config");
		WorldMapConfig.blockList = blockList.getSFSArray("blocklist");
		WorldMapConfig.cityList = cityList;
		WorldMapConfig.baseList = baseList;
		WorldMapConfig.baseMap = baseMap;
		WorldMapConfig.isLoaded = true;
	}

	/**
	 * 获取所有区块列表
	 */
	public static ISFSArray getBlockList() {
		return blockList;
	}

	/**
	 * 获取所有城池列表
	 */
	public static ISFSArray getAllCityList() {
		ISFSArray result = new SFSArray();
		// iterator
		Iterator<Entry<String, SFSDataWrapper>> iter = cityList.iterator();
		while (iter.hasNext()) {
			Entry<String, SFSDataWrapper> entry = iter.next();
			SFSDataWrapper dataWrapper = entry.getValue();
			if (dataWrapper.getTypeId() == SFSDataType.SFS_ARRAY) {
				ISFSArray arr = (ISFSArray) dataWrapper.getObject();
				int size = arr.size();
				for (int i = 0; i < size; ++i) {
					result.add(arr.get(i));
				}
			}
		}
		return result;
	}

	/**
	 * 获取所有据点列表
	 */
	public static ISFSArray getAllBaseList() {
		ISFSArray result = new SFSArray();
		for (String key : baseList.getKeys()) {
			ISFSArray arr = baseList.getSFSArray(key);
			for (int i = 0; i < arr.size(); ++i) {
				result.addSFSObject(arr.getSFSObject(i));
			}
		}
		return result;
	}

	/**
	 * 获取某区块城池列表
	 */
	public static ISFSArray getBlockCityList(int blockId) {
		String blockKey = "b" + blockId;
		if (cityList.containsKey(blockKey)) {
			return cityList.getSFSArray(blockKey);
		}
		return null;
	}

	/**
	 * 城池内所有据点列表
	 */
	public static ISFSArray getCityBaseList(int blockId, int cityId) {
		String cityKey = "B" + blockId + "c" + cityId;
		if (baseList.containsKey(cityKey)) {
			return baseList.getSFSArray(cityKey);
		}
		return null;
	}

	/**
	 * 通过blockId获取具体区块信息
	 */
	public static ISFSObject getBlockConfig(int blockId) {
		ISFSArray list = getBlockList();
		for (int i = 0; i < list.size(); ++i) {
			if (list.getSFSObject(i).getInt("blockId") == blockId) {
				return list.getSFSObject(i);
			}
		}
		return null;
	}

	/**
	 * 通过 区块-城池 定位副本,获取副本id
	 */
	public static final String getCityMissionId(int blockId, int cityId) {
		String blockMission = getBlockConfig(blockId).getUtfString("mission");
		String cityMission = getCityConfig(blockId, cityId).getUtfString("mission");
		return blockMission + cityMission;
	}

	/**
	 * 通过城池ID获取具体城池信息
	 */
	public static ISFSObject getCityConfig(int blockId, int cityId) {
		ISFSArray allData = getBlockCityList(blockId);
		for (int i = 0; i < allData.size(); ++i) {
			ISFSObject obj = allData.getSFSObject(i);
			if (obj.getInt("id") == cityId) {
				return obj;
			}
		}
		return null;
	}

	/**
	 * 获取该城池解锁条件中的 unlockCity
	 */
	public static final int getCityRequireUnlockCity(int blockId, int cityId) {
		return getCityConfig(blockId, cityId).getSFSObject("require").getInt("unlockCity");
	}

	/**
	 * 获取该城池解锁条件中的  (最小)指挥官等级 level
	 */
	public static final int getCityRequireCMDLevel(int blockId, int cityId) {
		return getCityConfig(blockId, cityId).getSFSObject("require").getInt("level");
	}

	/**
	 * 获取具体据点配置信息
	 */
	public static ISFSObject getBaseConfig(int blockId, int cityId, int baseId) {
		ISFSArray arr = getCityBaseList(blockId, cityId);
		for (int i = 0; i < arr.size(); ++i) {
			ISFSObject obj = arr.getSFSObject(i);
			if (obj.getInt("id") == baseId) {
				return obj;
			}
		}
		return null;
	}

	/**
	 * 通过城池ID和据点ID获取某城池具体据点地图
	 */
	public static final ISFSObject getBaseMap(int blockId, int cityId, int baseId) {
		String key = "B" + blockId + "c" + cityId + "b" + baseId;
		if (baseMap.containsKey(key)) {
			return baseMap.getSFSObject(key);
		}
		return null;
	}

	/**
	 * 获取据点内单个建筑的升级价格总数
	 */
	public static final ISFSObject getSingleBaseBDResSum(String bdCode, int level) {
		// 建筑列表以及升级参数
		ISFSObject ret = SFSObject.newInstance();
		for (int i = 0; i < level; ++i) { // 0 ~ (n) 之和,不等差也不等比
			// 获取对应资源以及种类
			ISFSObject bRes = BuildingConfig.getBDBaseUpgradeRes(bdCode, i);
			for (String resKey : bRes.getKeys()) {
				if (!ret.containsKey(resKey)) {
					ret.putInt(resKey, 0);
				}
				ret.putInt(resKey, ret.getInt(resKey) + bRes.getInt(resKey));
			}
		}
		return ret;
	}

	/**
	 * 获取据点建筑的匹配表
	 */
	public static final ISFSObject getBaseMapping() {
		return getBaseServerConfig().getSFSObject("mapping");
	}

	/**
	 * 获取据点能升级的最大等级
	 */
	public static final int getBaseMaxLevel() {
		return getBaseServerConfig().getInt("maxLv");
	}

	/**
	 * 获取据点建筑强弱序列,降序
	 */
	public static final List<Integer> getBaseBDGradient() {
		return new ArrayList<>(getBaseServerConfig().getIntArray("gradient"));
	}

	public static final ISFSObject getBaseServerRectCfg() {
		return getBaseServerConfig().getSFSObject("rect");
	}

	/**
	 * 获取据点配置数据
	 */
	private static final ISFSObject getBaseServerConfig() {
		return config.getSFSObject("base");
	}

	/**
	 * 获取城池配置数据
	 */
	private static final ISFSObject getCityServerConfig() {
		return config.getSFSObject("city");
	}

	public static final ISFSObject getCityTimeCfg() {
		return getCityServerConfig().getSFSObject("time");
	}

	/**
	 * 获取城池战斗冷却时间
	 */
	public static final int getCityBattleCD(int type) {
		String key = "cd" + "_" + type;
		return getCityServerConfig().getInt(key);
	}

	/**
	 * 获取据点战斗冷却时间
	 */
	public static final int getBaseBattleCD() {
		return getBaseServerConfig().getInt("cd");
	}

	/**
	 * 权限类别
	 * @author NorthLan
	 */
	public static enum PermType {
		DECLAR, // 宣战
		;
		@Override
		public String toString() {
			return super.toString().toLowerCase();
		};
	}

	public static final boolean hasPermission(PermType permType, int role) {
		return getCityServerConfig().getSFSObject("rolePerm").getSFSArray(permType.toString()).contains(role);
	}

	public static final boolean isCityCanDeclarByType(int type) {
		return getCityServerConfig().getSFSObject("typeLimit").getSFSArray("declar").contains(type);
	}
}
